package com.bs.auth.service.impl;

import com.bs.auth.utils.Assert;
import com.bs.auth.vo.UserVo;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

/**
 * @author bingshao
 * @date 2023/2/23
 **/
@Service("msgDetailsServiceImpl")
public class MsgDetailsServiceImpl implements UserDetailsService {

    @Resource
    private PasswordEncoder passwordEncoder;


    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        UserVo userVo = new UserVo();
        if ("17751335300".equals(username)) {
            // 普通用户，权限为1,2
            userVo.setId(1L);
            userVo.setUsername("妃妃");
            userVo.setPassword(passwordEncoder.encode("123456"));
            List<SimpleGrantedAuthority> list = new LinkedList<>();
            list.add(new SimpleGrantedAuthority("1"));
            list.add(new SimpleGrantedAuthority("2"));
            userVo.setAuthorities(list);
            return userVo;
        } else {
            Assert.isError("查无此人");
        }
        return userVo;

    }
}
